﻿using MovingChame.Web.Helpers;
using MovingChame.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MovingChame.Web.Infrastructure
{
    public class ConcreteTourProvider : IConcreteTourProvider
    {
        private static ConcreteTourProvider current;
        public static ConcreteTourProvider Current
        {
            get
            {
                if (current == null) current = new ConcreteTourProvider();
                return current;
            }
        }

        #region Configuration
        private string applicationName;
        public string ApplicationName
        {
            get { return applicationName; }
            set { applicationName = value; }
        }

        private string connectionStringName;
        private string providerName;
        private MovingChameModelContainer db;

        public void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            providerName = name;

            if (config["applicationName"] != null)
                applicationName = config["applicationName"];

            if (config["connectionStringName"] != null)
            {
                connectionStringName = config["connectionStringName"];
                db = new MovingChameModelContainer();
                db.Database.Connection.ConnectionString = ConfigurationHelper.GetConnectionString(connectionStringName);
            }
            else
                db = new MovingChameModelContainer();
        }

        private ConcreteTourProvider()
        {
            Initialize("ConcreteTourProvider", new System.Collections.Specialized.NameValueCollection());
        }
        #endregion

        #region Interface Methods
        public int AddConcreteTour(DateTime startDate, DateTime endDate, int abstractTourId)
        {
            var tour = new ConcreteTour
            {
                StartDate = startDate,
                EndDate = endDate,
                AbstractTourId = abstractTourId
            };

            db.ConcreteTours.Add(tour);
            db.SaveChanges();

            return tour.Id;
        }

        public bool UpdateConcreteTour(int concreteTourId, ConcreteTour updatedConcreteTour)
        {
            ConcreteTour tour = GetConcreteTourById(concreteTourId);
            if (tour == null)
                return false;
            db.Entry(tour).CurrentValues.SetValues(updatedConcreteTour);
            db.SaveChanges();
            return true;
        }

        public Boolean UpdateConcreteTourStatus(int concreteTourId, TourStatus status)
        {
            var concreteTour = GetConcreteTourById(concreteTourId);
            if (concreteTour == null)
                return false;
            concreteTour.Status = status;
            db.SaveChanges();
            return true;
        }

        public Boolean RemoveConcreteTour(int concreteTourId)
        {
            var tour = db.ConcreteTours.SingleOrDefault(o => o.Id == concreteTourId);
            if (tour == null)
                return false;

            db.ConcreteTours.Remove(tour);
            db.SaveChanges();
            return true;
        }

        public ConcreteTour GetConcreteTourById(int concreteTourId)
        {
            return db.ConcreteTours.SingleOrDefault(o => o.Id == concreteTourId);
        }

        public ConcreteTour GetLatestConcreteTourByAbstractTour(int abstractTourId)
        {
            return db.ConcreteTours.LastOrDefault(o => o.AbstractTourId == abstractTourId);
        }

        public IQueryable<ConcreteTour> GetConcreteTourByAbstractTour(int abstractTourId)
        {
            return db.ConcreteTours.Where(o => o.AbstractTourId == abstractTourId);
        }

        public AbstractTour GetAbstractTourByConcreteTour(int concreteTourId)
        {
            return db.ConcreteTours.Where(o => o.Id == concreteTourId).FirstOrDefault().AbstractTour;
        }

        #endregion
    }
}